<template>
  <div class="maintain-order-manage">
    <el-card shadow="hover">
      <div class="maintain-header">
        <el-input
          v-model="searchText"
          placeholder="搜索设备名称/编码/工单号"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-select v-model="searchStatus" placeholder="状态" clearable style="width:120px; margin-right:16px;">
          <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item"/>
        </el-select>
        <el-button type="primary" @click="handleAdd">新增工单</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="id" label="工单编号" width="120px"/>
        <el-table-column prop="deviceName" label="设备名称" width="140px"/>
        <el-table-column prop="deviceCode" label="设备编码" width="120px"/>
        <el-table-column prop="content" label="保养内容" width="200px"/>
        <el-table-column prop="maintainer" label="保养人" width="100px"/>
        <el-table-column prop="maintainTime" label="保养时间" width="160px"/>
        <el-table-column prop="status" label="状态" width="100px">
          <template #default="scope">
            <el-tag :type="statusTagType(scope.row.status)">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="140px"/>
        <el-table-column label="操作" width="160px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="100px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="editRow.deviceName"/>
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceCode">
          <el-input v-model="editRow.deviceCode"/>
        </el-form-item>
        <el-form-item label="保养内容" prop="content">
          <el-input v-model="editRow.content"/>
        </el-form-item>
        <el-form-item label="保养人" prop="maintainer">
          <el-input v-model="editRow.maintainer"/>
        </el-form-item>
        <el-form-item label="保养时间" prop="maintainTime">
          <el-date-picker v-model="editRow.maintainTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" style="width:100%;" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editRow.status" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该工单吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

interface MaintainOrder {
  id: string
  deviceName: string
  deviceCode: string
  content: string
  maintainer: string
  maintainTime: string
  status: string
  remark: string
}

const statusOptions = ['待处理', '处理中', '已完成', '已关闭']

// 15条模拟数据
const orderList = ref<MaintainOrder[]>([
  { id: 'MO001', deviceName: '中央空调', deviceCode: 'EQ001', content: '清洗滤网', maintainer: '张三', maintainTime: '2024-06-01 09:23:12', status: '待处理', remark: '' },
  { id: 'MO002', deviceName: '新风主机', deviceCode: 'EQ002', content: '更换风机轴承', maintainer: '李四', maintainTime: '2024-06-01 10:45:07', status: '处理中', remark: '' },
  { id: 'MO003', deviceName: '电梯', deviceCode: 'EQ003', content: '润滑安全钳', maintainer: '王五', maintainTime: '2024-06-01 11:23:18', status: '已完成', remark: '' },
  { id: 'MO004', deviceName: '消防泵', deviceCode: 'EQ004', content: '压力表校验', maintainer: '赵六', maintainTime: '2024-06-01 12:34:11', status: '待处理', remark: '' },
  { id: 'MO005', deviceName: '净水设备', deviceCode: 'EQ005', content: '更换滤芯', maintainer: '孙七', maintainTime: '2024-06-01 13:45:22', status: '已关闭', remark: '计划取消' },
  { id: 'MO006', deviceName: '照明系统', deviceCode: 'EQ006', content: '检查灯具', maintainer: '周八', maintainTime: '2024-06-02 08:16:15', status: '处理中', remark: '' },
  { id: 'MO007', deviceName: '中央空调', deviceCode: 'EQ001', content: '检修冷凝器', maintainer: '钱九', maintainTime: '2024-06-02 09:32:07', status: '待处理', remark: '' },
  { id: 'MO008', deviceName: '电梯', deviceCode: 'EQ003', content: '控制系统自检', maintainer: '张三', maintainTime: '2024-06-02 10:45:32', status: '处理中', remark: '' },
  { id: 'MO009', deviceName: '配电柜', deviceCode: 'EQ007', content: '紧固螺丝', maintainer: '李四', maintainTime: '2024-06-02 11:23:13', status: '已完成', remark: '' },
  { id: 'MO010', deviceName: '空气净化器', deviceCode: 'EQ008', content: '更换活性炭', maintainer: '王五', maintainTime: '2024-06-02 12:38:29', status: '处理中', remark: '' },
  { id: 'MO011', deviceName: '中央空调', deviceCode: 'EQ001', content: '检查温控', maintainer: '赵六', maintainTime: '2024-06-03 08:23:48', status: '待处理', remark: '' },
  { id: 'MO012', deviceName: '电梯', deviceCode: 'EQ003', content: '门系统调试', maintainer: '孙七', maintainTime: '2024-06-03 09:17:16', status: '处理中', remark: '' },
  { id: 'MO013', deviceName: '照明系统', deviceCode: 'EQ006', content: '更换灯管', maintainer: '周八', maintainTime: '2024-06-03 10:28:45', status: '已完成', remark: '' },
  { id: 'MO014', deviceName: '空气净化器', deviceCode: 'EQ008', content: '传感器检查', maintainer: '钱九', maintainTime: '2024-06-03 11:32:20', status: '已关闭', remark: '' },
  { id: 'MO015', deviceName: '配电柜', deviceCode: 'EQ007', content: '检测接地', maintainer: '张三', maintainTime: '2024-06-03 12:23:14', status: '待处理', remark: '' }
])

// 搜索
const searchText = ref('')
const searchStatus = ref('')
const filteredList = computed(() => {
  let arr = orderList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.deviceName.includes(searchText.value) ||
        r.deviceCode.includes(searchText.value) ||
        r.id.includes(searchText.value)
    )
  }
  if (searchStatus.value) {
    arr = arr.filter(r => r.status === searchStatus.value)
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增工单')
const editRow = reactive<MaintainOrder>({
  id: '',
  deviceName: '',
  deviceCode: '',
  content: '',
  maintainer: '',
  maintainTime: '',
  status: '',
  remark: ''
})
const formRef = ref()
const formRules = {
  deviceName: [{ required: true, message: '必填', trigger: 'blur' }],
  deviceCode: [{ required: true, message: '必填', trigger: 'blur' }],
  content: [{ required: true, message: '必填', trigger: 'blur' }],
  maintainer: [{ required: true, message: '必填', trigger: 'blur' }],
  maintainTime: [{ required: true, message: '必填', trigger: 'change' }],
  status: [{ required: true, message: '必选', trigger: 'change' }]
}

function handleAdd() {
  dialogTitle.value = '新增工单'
  Object.assign(editRow, { id: '', deviceName: '', deviceCode: '', content: '', maintainer: '', maintainTime: '', status: '', remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: MaintainOrder) {
  dialogTitle.value = '编辑工单'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = orderList.value.findIndex(t => t.id === editRow.id)
      if (idx !== -1) orderList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      // 自动生成新编号
      editRow.id = 'MO' + (orderList.value.length + 1).toString().padStart(3, '0')
      orderList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

// 标签类型
const statusTagType = (status: string | undefined): 'success' | 'warning' | 'danger' | 'info' | 'primary' | undefined => {
  switch (status) {
    case '待处理': return 'warning'
    case '处理中': return 'primary'
    case '已完成': return 'success'
    case '已关闭': return 'info'
    default: return 'primary'
  }
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<MaintainOrder | null>(null)
function handleDelete(row: MaintainOrder) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    orderList.value = orderList.value.filter(t => t.id !== deleteTarget.value!.id)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.maintain-order-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.maintain-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
